package com.rt.easyjava.common.utils.others;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {


	public static boolean isNull(Object value){
		if (value == null) {
			return true;
		}

		if (value instanceof String) {
			if (((String) value).trim().equals("")) {
				return true;
			}
		}

		return false;
	}

	public static boolean areNotEmpty(Object value1, Object value2){
		if (!isNull(value1) && !isNull(value2)) {
			return true;
		}

		return false;
	}


	/**
	 * 异常时返回参数值
	 * @param s
	 * @param returnValue4Exception
	 * @return
	 */
	public static Integer parseInteger(String s, Integer returnValue4Exception){
		try{
			return Integer.parseInt(s);
		}catch (Exception e){
			return returnValue4Exception;
		}
	}

	/**
	 * 异常时返回参数值
	 * @param s
	 * @param returnValue4Exception
	 * @return
	 */
	public static Long parseLong(String s, Long returnValue4Exception){
		try{
			return Long.parseLong(s);
		}catch (Exception e){
			return returnValue4Exception;
		}
	}


	/**
	 * 字段转成属性
	 * @param columnName
	 * @return
	 */
	public static String switch2Property(String columnName){
		//System.out.println(columnName.matches("c+"));
		if (columnName.contains("_")){
			Pattern pattern = Pattern.compile("_[a-z,0-9]");
			Matcher matcher = pattern.matcher(columnName);
			while(matcher.find()){
				String old = matcher.group();
				String new1 = matcher.group().toUpperCase();
				columnName = columnName.replaceAll(old, new1.replace("_",""));
			}
		}
		return columnName;


	}


	/**
	 * 属性转字段
	 * @param property
	 * @return
	 */
	public static String switch2Column(String property){
		if (property.matches("[a-z]+[A-Z][a-z]+([A-Z][a-z]+)*")) {
			Pattern pattern = Pattern.compile("[A-Z]");
			Matcher matcher = pattern.matcher(property);
			while(matcher.find()){
				String old = matcher.group();
				System.out.println(old);
				String ne = matcher.group().toLowerCase();
				property = property.replaceAll(old, "_"+ne);
			}
		}
		return property;
	}


}
